package action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Shop;
import service.BuyerService;
import service.SellerService;
import util.PathTool;

/**
 * 卖家发货
 * 
 * Servlet implementation class OrderServlet
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("login") != null && (boolean)session.getAttribute("login")) { 
			String myId = (String)session.getAttribute("myId"); // 卖家Id
			Shop[] myShop = SellerService.getAllOrder(myId); // 所有订单
			String message = "";
			for (int i=0; i<myShop.length; i++) {
				
				message = message + "<tr><th>"
						+ myShop[i].getProductId()
						+ "</th><th>"
						+ myShop[i].getAmount()
						+ "</th><th>"
						+ myShop[i].getBuyerId()
						+ "</th><th>"
						+ SellerService.delivery(myShop[i].getProductId(), myId, myShop[i].getBuyerId())
						+ "</th></tr>";
			}
			String temp = "<table border=\"1\">"
					+ "<tr><th>ProductId</th><th>amount</th><th>BuyerName</th><th>Status</th></tr>"
					+ "";
			response.getWriter().write(temp + message + "</table> <br/> <a href=\""
					+ request.getHeader("Referer")
					+ "\">&nbsp;&nbsp;&nbsp;Return</a>");
		} else { // 未登录
			String message = "<a href=\"./"
					+ PathTool.getFileName(request.getHeader("Referer"))
					+ "\">&nbsp;&nbsp;&nbsp;Return</a>";
			response.getWriter().write("not login. <br/>" + message);
			System.out.println(PathTool.getFileName(request.getHeader("Referer")));
		}
	}

}
